
var MyDataGrid=function (prefix){	
		this.prefix=prefix;
		this._grid=$("#"+prefix);
		
		this.pager={			
			totalPage:5,
			curPage:2,
			rowsOfPage:50				
		};
		this.sort={
			sortColumn:"unit",
			sortRule:"desc"
		};
	};
	
	MyDataGrid.prototype={			
		getPager:function(){
			return this.pager;	
		},
		setPager:function(pager){
			this.pager=pager;
		},
		getSort:function(){
			return this.sort;
		},
		setSort:function(sort){
			this.sort=sort;
		},		
		init:function(){
			var _self=this;		
			
			$(document).click(function(){
				$(".x-column-header-inner",_self).removeClass("x-column-header-open");		
				var defaultPropSh={left: '645px',top: '251px'};
				var defaultPropMn={left: '645px',top: '247px'};
				$("#"+_self.prefix+"_popmenu_shade").css(defaultPropSh).hide();
				$("#"+_self.prefix+"_popmenu").css(defaultPropMn).hide();
			});
						
			//鼠标移动到行上变样式
			$(".x-grid-row",this._grpid).hover(function(){
				$(this).addClass("x-grid-row-over");			
			},function(){
				$(this).removeClass("x-grid-row-over");			
			});
			//表格title变色			
			jQuery(".x-column-header-inner" ,this._grid).hover(function(){
				$(this).addClass("x-column-header-over");			
			},function(){			
				$(this).removeClass("x-column-header-over");			
			});
			//排序
			$(".x-column-header-inner",this._grid).each(function(){				
				if($(this).hasClass("sorting")){
					$(this).click(function(){
						var orderColumn=$(this).attr("id");	
						var sortRule="";
						if(_self.sort.sortColumn.toLowerCase()!=orderColumn.toLowerCase()){
							sortRule="asc";
						}else{
							sortRule=_self.sort.sortRule.toLowerCase()==""?"asc":_self.sort.sortRule.toLowerCase()=="asc"?"desc":"";							
						}
						
						eval(_self.prefix+"SortRequest('"+orderColumn+"','"+sortRule+"')");				
					});			
				}
			});
			
			//弹出菜单
			$(".x-column-header-trigger",this._grid).click(function(){		
				var flg=$(this).parent("div").hasClass("x-column-header-open");
								
				if(flg){				
					$(this).parent("div").removeClass("x-column-header-open");	
					var defaultPropSh={left: '645px',top: '251px'};
					var defaultPropMn={left: '645px',top: '247px'};
					$("#"+_self.prefix+"_popmenu_shade").css(defaultPropSh).hide();
					$("#"+_self.prefix+"_popmenu").css(defaultPropMn).hide();
				}else{
					$(".x-column-header-inner",_self).removeClass("x-column-header-open");		
					$(this).parent("div").addClass("x-column-header-open");				
					var top=$(this).offset().top+$(this).height();
					var left=$(this).offset().left;
					var shProp={left:left+"px",top:top+"px"};
					var mnProp={left:left+"px",top:top+"px"};
					$("#"+_self.prefix+"_popmenu_shade").css(shProp).show();
					$("#"+_self.prefix+"_popmenu").css(mnProp).show();
				}
				return false;
			});
			
			
			//鼠标移到菜单项上变样式
			$(".x-menu-item",this._grid.nextAll("#"+_self.prefix+"_popmenu")).hover(function(){
				$(this).addClass("x-menu-item-active");			
			},function(){
				$(this).removeClass("x-menu-item-active");			
			});
			//菜单上的checkbox
			$(".x-menu-item-checked",this._grid.nextAll("#"+_self.prefix+"_popmenu")).toggle(function(){
				$(this).removeClass("x-menu-item-checked");			
				$(this).addClass("x-menu-item-unchecked");
				alert($(this).find("span").attr("id")+" is unchecked!");
			},function(){
				$(this).removeClass("x-menu-item-unchecked");
				$(this).addClass("x-menu-item-checked");	
				alert($(this).find("span").attr("id")+" is checked!");
			});		
			
			var _pagerBar=$("#"+this.prefix+"_pagingtoolbar");
			$("#ipt_pageNo",_pagerBar).val(this.pager.curPage);
			$("#pager_totalpage_text",_pagerBar).html(" of "+this.pager.totalPage);
			
			if(this.pager.curPage==1){
				$("#pager_first_btn",_pagerBar).attr("disabled","disabled").attr("hidefocus","true");
				$("#pager_first_div",_pagerBar).addClass("x-btn-disabled").addClass("x-item-disabled");
				
				$("#pager_prev_btn",_pagerBar).attr("disabled","disabled").attr("hidefocus","true");
				$("#pager_prev_div",_pagerBar).addClass("x-btn-disabled").addClass("x-item-disabled");
				
				if(this.pager.totalPage==1){
					$("#pager_next_btn",_pagerBar).attr("disabled","disabled").attr("hidefocus","true");
					$("#pager_next_div",_pagerBar).addClass("x-btn-disabled").addClass("x-item-disabled");
					
					$("#pager_last_btn",_pagerBar).attr("disabled","disabled").attr("hidefocus","true");
					$("#pager_last_div",_pagerBar).addClass("x-btn-disabled").addClass("x-item-disabled");
					
				}else{
					$("#pager_next_btn",_pagerBar).click(function(){						
						eval(_self.prefix+"NextRequest("+(_self.pager.curPage+1)+")");
					});
					
					$("#pager_last_btn",_pagerBar).click(function(){
						eval(_self.prefix+"LastRequest("+_self.pager.totalPage+")");
					});
				}				
			}else{
				$("#pager_first_btn",_pagerBar).click(function(){
					eval(_self.prefix+"FirstRequest(1)");
				});
				
				$("#pager_prev_btn",_pagerBar).click(function(){
					eval(_self.prefix+"PrevRequest("+(_self.pager.curPage-1)+")");
				});		
				
				if(this.pager.curPage==this.pager.totalPage){
					$("#pager_next_btn",_pagerBar).attr("disabled","disabled").attr("hidefocus","true");
					$("#pager_next_div",_pagerBar).addClass("x-btn-disabled").addClass("x-item-disabled");
					
					$("#pager_last_btn",_pagerBar).attr("disabled","disabled").attr("hidefocus","true");
					$("#pager_last_div",_pagerBar).addClass("x-btn-disabled").addClass("x-item-disabled");
				}else{
					$("#pager_next_btn",_pagerBar).click(function(){
						eval(_self.prefix+"NextRequest("+(_self.pager.curPage+1)+")");
					});
					
					$("#pager_last_btn",_pagerBar).click(function(){
						eval(_self.prefix+"LastRequest("+_self.pager.totalPage+")");
					});
					
				}
			}	
			
			if(this.sort.sortColumn.length!=0){
				$(".x-column-header-inner",this._grid).each(function(){	
					if($(this).hasClass("sorting")){
						var orderColumn=$(this).attr("id");						
						if(orderColumn.toLowerCase()==_self.sort.sortColumn.toLowerCase()){
							$(this).parent("div").addClass("x-column-header-sort-"+_self.sort.sortRule.toUpperCase());
						}
					}
				});
			}
			
		}		
	};
